home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / comms / netsoftware / amitcp2_x_gcc.lha / autoinit.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  5KB  |  202 lines

  1. char RCS_ID_AUTOINIT_C[] = "$Id: autoinit.c,v 1.7 1994/02/04 01:47:09 jasegler Exp jasegler $";
  2. /*
  3.  * autoinit.c --- SAS C auto initialization functions
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  8.  *                  Helsinki University of Technology, Finland.
  9.  *                  All rights reserved.
  10.  *
  11.  * Created      : Sat Mar 20 03:31:29 1993 ppessi
  12.  * Last modified: Mon Jul 19 14:24:30 1993 jraja
  13.  *
  14.  */
  15.  
  16. #include <dos/dos.h>
  17. #include <sys/types.h>
  18. #include <devices/timer.h>
  19. #include <exec/types.h>
  20. #include <exec/libraries.h>
  21.  
  22. #include <intuition/intuition.h>
  23.  
  24. #ifdef __GNUC__            /* C= include files only have clib not Sux's (SASC) proto files :) */
  25. #include <clib/exec_protos.h>
  26. #include <clib/intuition_protos.h>
  27. #include <clib/socket_protos.h>
  28. #include <signal.h>
  29. #elif _DCC
  30. #include <clib/socket_protos.h>
  31. #include <clib/exec_protos.h>
  32. #include <clib/intuition_protos.h>
  33. #elif __SASC__
  34. #include <proto/socket.h>
  35. #include <proto/exec.h>
  36. #include <proto/intuition.h>
  37. #endif
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41.  
  42. struct Library *SocketBase = NULL;
  43.  
  44. int errno = 0;            /* global errno variable */
  45.  
  46. STRPTR SOCKETNAME = "bsdsocket.library";
  47.  
  48. #define SOCKETVERSION 2        /* minimum bsdsocket version to use */
  49.  
  50. #ifdef __SASC__
  51. extern STRPTR _ProgramName;    /* SAS startup module defines this :-) */
  52. #endif
  53.  
  54. #ifdef __GNUC__
  55. char _ProgramName[] = "GNU C Program";
  56. #endif
  57.  
  58. #ifdef _DCC
  59. char _ProgramName[] = "DCC Program";
  60. #endif
  61.  
  62. /****** amiga.lib/autoinit *********************************************
  63.  
  64.  *   NAME
  65.  *       autoinit - SAS C Autoinitialization Functions
  66.  *
  67.  *   SYNOPSIS
  68.  *       _STIopenSockets()
  69.  *
  70.  *       void _STIopenSockets(void)
  71.  *
  72.  *       _STDcloseSockets()
  73.  *
  74.  *       void _STDcloseSockets(void)
  75.  *
  76.  *   FUNCTION
  77.  *       These functions open and close the bsdsocket.library at the
  78.  *       startup and exit of the program, respectively. For a
  79.  *       program to use these functions, it must be linked with
  80.  *       netlib:net.lib.
  81.  *
  82.  *       If the library can be opened, the _STIopenSockets() calls
  83.  *       bsdsocket.library function SetErrnoPtr() to tell the
  84.  *       library the address and the size of the errno variable of
  85.  *       the calling program.
  86.  *
  87.  *   NOTES
  88.  *       _STIopenSockets() also checks that the system version is at
  89.  *       least 37. It puts up a requester if the bsdsocket.library
  90.  *       is not found or is of wrong version.
  91.  *
  92.  *       The autoinitialization and autotermination functions are
  93.  *       features specific to the SAS C6. However, these functions
  94.  *       can be used with other (ANSI) C compilers, too. Example
  95.  *       follows:
  96.  *
  97.  *       \* at start of main() *\
  98.  *
  99.  *       atexit(_STDcloseSockets);
  100.  *       _STDopenSockets();
  101.  *
  102.  *   BUGS
  103.  *
  104.  *   SEE ALSO
  105.  *       bsdsocket.library/SetErrnoPtr(),
  106.  *       SAS/C 6 User's Guide p. 145 for details of
  107.  *       autoinitialization and autotermination functions.
  108.  *****************************************************************************
  109.  *
  110.  */
  111.  
  112. void
  113. _STIopenSockets (void)
  114. {
  115.   static short done = 0;
  116. #ifdef __SASC__
  117.   struct Library *IntuitionBase;
  118. #endif
  119.   STRPTR errorStr;
  120.  
  121.   if (done++)            /* try only once (SAS/C 6.2 liked to call this twice) */
  122.     return;
  123.  
  124.   /*
  125.    * Check OS version
  126.    */
  127.   if ((*(struct Library **) 4)->lib_Version < 37)
  128.     exit (20);
  129.  
  130.   /*
  131.    * Open bsdsocket.library
  132.    */
  133.   if ((SocketBase = OpenLibrary (SOCKETNAME, SOCKETVERSION)) != NULL)
  134.     {
  135.       /*
  136.          * Succesfull. Now tell bsdsocket.library the address of our errno
  137.        */
  138.       SetErrnoPtr (&errno, sizeof (errno));
  139.  
  140.       return;
  141.     }
  142.   else
  143.     {
  144.       errorStr = "AmiTCP/IP version 2 or later must be started first.";
  145.     }
  146.  
  147. #ifdef __SASC__
  148.   IntuitionBase = OpenLibrary ("intuition.library", 36);
  149.  
  150.   if (IntuitionBase != NULL)
  151.     {
  152.       struct EasyStruct libraryES;
  153.  
  154.       libraryES.es_StructSize = sizeof (libraryES);
  155.       libraryES.es_Flags = 0;
  156.       libraryES.es_Title = _ProgramName;
  157.       libraryES.es_TextFormat = errorStr;
  158.       libraryES.es_GadgetFormat = "Exit %s";
  159.  
  160.       EasyRequestArgs (NULL, &libraryES, NULL, (APTR) & _ProgramName);
  161.  
  162.       CloseLibrary (IntuitionBase);
  163.     }
  164.   exit (20);
  165. #endif
  166.  
  167. #ifdef __GNUC__
  168.   fprintf (stderr, "%s\n\n", errorStr);
  169.   exit (20);
  170. #endif
  171. }
  172.  
  173. void
  174. _STDcloseSockets (void)
  175. {
  176.   if (SocketBase)
  177.     {
  178.       CloseLibrary (SocketBase);
  179.       SocketBase = NULL;
  180.     }
  181. }
  182.  
  183. #ifdef __GNUC__
  184.  
  185. static void
  186. constructor ()
  187. {
  188.   _STIopenSockets ();
  189.   SetSocketSignals (SIGBREAKF_CTRL_C, SIGIO, SIGURG);
  190. }
  191.  
  192. static void
  193. destructor ()
  194. {
  195.   _STDcloseSockets ();
  196. }
  197.  
  198. asm ("  .text;  .stabs \"___CTOR_LIST__\",22,0,0,_constructor");
  199. asm ("  .text;  .stabs \"___DTOR_LIST__\",22,0,0,_destructor");
  200.  
  201. #endif /* GNUC Auto openers */
  202.